package com.voiceqsologger.controller;

import com.voiceqsologger.service.AudioDeviceService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.sound.sampled.TargetDataLine;
import java.util.List;
import java.util.Map;

/**
 * 音频设备管理控制器
 */
@RestController
@RequestMapping("/api/audio")
@RequiredArgsConstructor
public class AudioDeviceController {

    private final AudioDeviceService audioDeviceService;

    /**
     * 获取所有音频输入设备列表
     */
    @GetMapping("/devices")
    public List<AudioDeviceService.AudioDeviceInfo> listDevices() {
        return audioDeviceService.listInputDevices();
    }

    /**
     * 测试指定音频设备是否可用
     */
    @GetMapping("/test")
    public Map<String, Object> testDevice(@RequestParam String deviceName) {
        try {
            TargetDataLine line = audioDeviceService.openInputDevice(deviceName);
            line.close();
            return Map.of(
                "success", true,
                "message", "设备可用",
                "deviceName", deviceName
            );
        } catch (Exception e) {
            return Map.of(
                "success", false,
                "message", "设备不可用: " + e.getMessage(),
                "deviceName", deviceName
            );
        }
    }
}
